package com.ewzj.bigdata.mvp.ui.adapter

import android.annotation.TargetApi
import android.graphics.Color
import android.os.Build
import android.support.annotation.RequiresApi
import android.view.View
import android.widget.ImageView
import android.widget.TextView

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.extensions.loadImgwithloading
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.model.rspbean.HomeBean
import com.ewzj.bigdata.mvp.ui.activity.HomeWebActivity
import com.jess.arms.base.BaseApplication
import com.jess.arms.utils.UiUtils
import com.jess.arms.widget.imageloader.ImageLoader
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

/**
 * author:zhangjing
 * 作用:
 * return:
 */

class HomePageAdapter(data: List<HomeBean>) : BaseMultiItemQuickAdapter<HomeBean, BaseViewHolder>(data) {
    private val TYPE_SECTION = 0
    //    新房
    private val TYPE_NEW = 1
    //    二手房
    private val TYPE_OLD = 2
    //    租房
    private val TYPE_RENT = 3
    private val str = "/"
    var refreshdataListener: RefreshdataListener? = null
    init {
        addItemType(TYPE_SECTION, R.layout.item_sectiontag)
        addItemType(TYPE_NEW, R.layout.item_houretype1)
        addItemType(TYPE_OLD, R.layout.item_houretype2)
        addItemType(TYPE_RENT, R.layout.item_houretype3)
    }

    override fun convert(baseViewHolder: BaseViewHolder, hourseBean: HomeBean) {
        if (baseViewHolder.layoutPosition == 0) {
            baseViewHolder.getView<View>(R.id.tv_diver).visibility = View.GONE
        } else {
            baseViewHolder.getView<View>(R.id.tv_diver).visibility = View.VISIBLE
        }
        if (baseViewHolder.itemViewType != 0) {
            baseViewHolder.getConvertView().setOnClickListener {
                try {
                    hourseBean.visit=(hourseBean.visit.toInt()+1).toString()
                }catch (e:Exception){
                    e.printStackTrace()
                }
                HomeWebActivity.startHomeActivity(mContext,hourseBean)
                baseViewHolder.convertView.post({
                    notifyDataSetChanged()
                })
            }

        }
        when (baseViewHolder.itemViewType) {
            0 -> {
                baseViewHolder.setText(R.id.tv_sectiontag, hourseBean.section)
                if (hourseBean.section == "为您推荐") {
                    baseViewHolder.setImageResource(R.id.iv_sectiontag, R.drawable.icon_star)
                } else if (hourseBean.section == "二手房") {
                    baseViewHolder.setImageResource(R.id.iv_sectiontag, R.drawable.icon_house_2)
                } else if (hourseBean.section == "新房") {
                    baseViewHolder.setImageResource(R.id.iv_sectiontag, R.drawable.icon_new1)
                } else if (hourseBean.section == "租房") {
                    baseViewHolder.setImageResource(R.id.iv_sectiontag, R.drawable.icon_zufang)
                }

                baseViewHolder.getConvertView().setOnClickListener {
                    if (refreshdataListener != null) {
                        refreshdataListener!!.refreshdata(hourseBean.section)
                    }
                }
            }
            1 -> {
                (baseViewHolder.getView<View>(R.id.iv_hourse) as ImageView).loadImgwithloading(hourseBean.thumb)
                baseViewHolder.setText(R.id.tv_location, hourseBean.region + "(" + hourseBean.address + ")")
                baseViewHolder.setText(R.id.tv_name, hourseBean.name)
                baseViewHolder.setText(R.id.area, hourseBean.area)
                baseViewHolder.setText(R.id.tv_watch, hourseBean.visit)
                baseViewHolder.setText(R.id.tv_price, hourseBean.price)
                baseViewHolder.setText(R.id.tv_source, hourseBean.sources)
                val tagFlowLayout = baseViewHolder.getView<TagFlowLayout>(R.id.tag_flow)
                tagFlowLayout.adapter = object : TagAdapter<HomeBean.HouseTagsEntity>(hourseBean.house_tags) {

                    override fun getView(parent: FlowLayout, position: Int, houseTagsEntity: HomeBean.HouseTagsEntity): View {
                        val tv = View.inflate(mContext, R.layout.item_tag_flow, null) as TextView
                        tv.background = Apputils.Utils.getBackgroundDrawable(parseColor(houseTagsEntity.color), parseColor(houseTagsEntity.color), 1, 10f)
                        tv.text = houseTagsEntity.tag_name
                        return tv
                    }
                }
            }
            2 -> {
                //                二手房
                (baseViewHolder.getView<View>(R.id.iv_hourse) as ImageView).loadImgwithloading(hourseBean.thumb)
                baseViewHolder.setText(R.id.tv_source, hourseBean.sources)
                baseViewHolder.setText(R.id.tv_name, hourseBean.name)
                baseViewHolder.setText(R.id.tv_price_count, hourseBean.total_price)
                baseViewHolder.setText(R.id.tv_price_per, hourseBean.price)
                baseViewHolder.setText(R.id.tv_watch, hourseBean.visit)
                baseViewHolder.setText(R.id.tv_location, hourseBean.layout + str + hourseBean.area + str + hourseBean.house_orien + str + hourseBean.house_name)
                Apputils.Utils.loadImgwithloading(mContext, hourseBean.thumb, baseViewHolder.getView<View>(R.id.iv_hourse) as ImageView)
                val tagFlowLayoutold = baseViewHolder.getView<TagFlowLayout>(R.id.tag_flow)
                tagFlowLayoutold.adapter = object : TagAdapter<HomeBean.HouseTagsEntity>(hourseBean.house_tags) {

                    override fun getView(parent: FlowLayout, position: Int, houseTagsEntity: HomeBean.HouseTagsEntity): View {
                        val tv = View.inflate(mContext, R.layout.item_tag_flow, null) as TextView
                        tv.background = Apputils.Utils.getBackgroundDrawable(parseColor(houseTagsEntity.color), parseColor(houseTagsEntity.color), 1, 10f)
                        tv.text = houseTagsEntity.tag_name
                        return tv
                    }
                }
            }
            3 -> {
                (baseViewHolder.getView<View>(R.id.iv_hourse) as ImageView).loadImgwithloading(hourseBean.thumb)
                baseViewHolder.setText(R.id.tv_source, hourseBean.sources)
                baseViewHolder.setText(R.id.area, hourseBean.layout + str + hourseBean.area + str + hourseBean.house_orien)
                baseViewHolder.setText(R.id.tv_location, hourseBean.region + " " + hourseBean.house_name)
                baseViewHolder.setText(R.id.tv_pricerent, hourseBean.price)
                baseViewHolder.setText(R.id.tv_watch, hourseBean.visit)
                if (hourseBean.rent_type == "entire") {
                    Apputils.Utils.lefttopTextview(baseViewHolder.getView<View>(R.id.tv_name) as TextView, hourseBean.name, R.drawable.icon_zhegnzu)
                } else if (hourseBean.rent_type == "share") {
                    Apputils.Utils.lefttopTextview(baseViewHolder.getView<View>(R.id.tv_name) as TextView, hourseBean.name, R.drawable.icon_hezu)
                } else {
                    baseViewHolder.setText(R.id.tv_name, hourseBean.name)
                }
                val tagFlowLayoutrent = baseViewHolder.getView<TagFlowLayout>(R.id.tag_flow)
                tagFlowLayoutrent.adapter = object : TagAdapter<HomeBean.HouseTagsEntity>(hourseBean.house_tags) {
                    override fun getView(parent: FlowLayout, position: Int, houseTagsEntity: HomeBean.HouseTagsEntity): View {
                        val tv = View.inflate(mContext, R.layout.item_tag_flow, null) as TextView
                        tv.background = Apputils.Utils.getBackgroundDrawable(parseColor("#ffffff"), parseColor(houseTagsEntity.color), 1, 10f)
                        tv.setTextColor(parseColor(houseTagsEntity.color))
                        tv.text = houseTagsEntity.tag_name
                        return tv
                    }
                }
            }
        }
    }

    interface RefreshdataListener {
        fun refreshdata(string: String)
    }

    private fun parseColor(color: String): Int {
        return Color.parseColor(color)
    }
}
